package com.wyh.seata.service.impl;

import com.wyh.seata.dao.OrderMapper;
import com.wyh.seata.domain.Order;
import com.wyh.seata.service.AccountService;
import com.wyh.seata.service.OrderService;
import com.wyh.seata.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author wyh
 * @version 1.0
 * @date 2021/12/1 16:35
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AccountService accountService;

    @Autowired
    private StorageService storageService;

    /**
     * 创建订单
     * 创建订单 - 修改库存 - 修改账户金额 - 修改订单状态
     * @param order 订单
     */
    @Override
    @GlobalTransactional(name = "wyh-create-order", rollbackFor = Exception.class)
    public void createOrder(Order order) {
        log.info("--->开始新建订单");
        long start = System.currentTimeMillis();
        orderMapper.createOrder(order);
        log.info("OrderServiceImpl---执行了: createOrder"+order);
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("OrderServiceImpl---执行了: decrease 参数为: ProductId--"+order.getProductId()+"Count--"+order.getCount());
        accountService.deMoney(order.getUserId(),order.getMoney());
        log.info("OrderServiceImpl---执行了: deMoney 参数为: UserId--"+order.getUserId()+"Money--"+order.getMoney());
        orderMapper.updateOrder(order.getUserId(),0);
        log.info("OrderServiceImpl---执行了: updateOrder  id为: "+order.getUserId()+"  status为: "+0);
        log.info("<---执行时间为: "+(start-System.currentTimeMillis()));
    }


}
